import datetime

from sqlalchemy import BigInteger, String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.core.models.enums.user import LangCode
from src.database.models.base import TimedBaseModel
from src.database.models.m2ms import promocodes_users


class User(TimedBaseModel):
    __tablename__ = "user"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    telegram_id: Mapped[int] = mapped_column(BigInteger, unique=True)
    username: Mapped[str] = mapped_column(String(50))
    balance: Mapped[float] = mapped_column(server_default="0")
    orders = relationship("Order", back_populates="user")
    promocodes = relationship(
        "Promocode", secondary=promocodes_users, back_populates="users"
    )
    bills = relationship("Bill", back_populates="user")
    lang_code: Mapped[LangCode] = mapped_column(server_default="ru")
    was_registered: Mapped[datetime.datetime] = mapped_column(server_default=func.now())

    def __repr__(self):
        return f"<Пользователь №{self.id} {self.telegram_id}>"

    def __str__(self):
        return self.__repr__()
